//
// Created by user on 2022/3/3.
//
#include <vector>
#include <algorithm>
using namespace std;
//前缀和
double findMaxAverage1(vector<int>& nums, int k) {
    int n = nums.size();
    vector<int> prexNum(n+1);
    prexNum[0] = 0;
    for (int i = 0; i <n ; ++i) {
        prexNum[i+1] = nums[i]+prexNum[i];
    }
    int ans = INT_MIN;

    for (int i = 0; i < nums.size()-k+1; ++i) {
        int sum = prexNum[i+k]-prexNum[i];
        ans = max(sum, ans);
    }
    return (double) ans/k;
}

double findMaxAverage(vector<int>& nums, int k) {
    int ans = INT_MIN;
    int left =0;
    int right = 0;
    int windowsum = 0;

    while (right<nums.size()){
        windowsum+=nums[right];
        //判断是否满足窗口条件：达到了固定的窗口大小
        if(right-left+1==k){
            ans = max(ans, windowsum);
            windowsum-=nums[left];
            left++;
        }
        right++;
    }
    return (double) ans/k;
}
